The modulus operator (%) returns the remainder when one integer is divided by another.
We can use the modulus operator to see if a number is even or odd. The computer is not able to simply look at a number and determine whether It is even or odd. We have to use arithmetic to test to see if it is even or odd.
When dividing by 10, if a number ends in a 0, this division leaves a remainder of 0. If a number does not end in 0, dividing by 10 leaves a remainder of whatever the rightmost digit is. For example, 11 divided by 10 leaves a remainder of 1. 12 divided by 10 leaves a remainder of 2. 13 divided by 10 leaves a remainder of 3. In every case, dividing by 10 leaves a remainder that is simply the rightmost digit of the dividend.
The division operator (/) returns the number of times the divisor (number to the left of the operator) goes into the dividend (number to the right of the operator). The number produced is called the quotient.
Normally we expect that if the dividend does not divide equally into the divisor, the quotient will have a fractional part (e.g., 5 / 2 = 2.5).
In C++, the division operator behaves slightly differently if both the divisor and dividend are integers. If both the divisor and dividend are integers, the division operator returns only the integer part of the quotient (e.g., under integer division, 5 / 2 = 2).
If a precise quotient is needed when dividing one integer by another, there are techniques to produce a precise quotient.
One of the tricky things about learning programming is that we need to give the computer step-by-step instructions for doing things that we are not used to thinking about as a process. For example, if I want the computer to tell me if a number is even or odd, I have to tell it to see what the remainder is when the number is divided by 2. We have to translate our human reasoning into steps and logic the computer can implement.
We use a combination of mods and integer division to perform different tasks. For example, if I want to know if the second digit of a three-digit number, say 345, is even, I might to do this:
Be mindful that people learning to program often struggle with the topics described above. Try to practice problems using these operators and techniques so that you can become comfortable with them.